package com.internetCafes.spms.web.sys.controller;

import java.util.*;

import com.internetCafes.spms.common.CommonConstants;
import com.internetCafes.spms.common.utils.ShiroUtils;
import com.internetCafes.spms.web.sys.model.UserInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.internetCafes.spms.core.model.pageModel.DataGrid;

import com.internetCafes.spms.web.sys.model.ConfInfo;
import com.internetCafes.spms.web.sys.service.ConfInfoService;
import com.internetCafes.spms.core.utils.Query;
import com.internetCafes.spms.core.utils.R;

import javax.servlet.http.HttpServletRequest;




/**
 * 系统配置表
 * 
 * @author Sunny
 * @email rekeeper2011@hotmail.com
 * @date 2018-11-12 22:26:42
 */
@RestController
@RequestMapping("/sys/confInfo")
public class ConfInfoController {

	@Autowired
	private ConfInfoService confInfoService;



	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sys:confinfo:list")
	public R list(@RequestParam Map<String, Object> params, HttpServletRequest request){

        //查询列表数据
        Query query = new Query(params);
		params = new HashMap<>();

		String type = request.getParameter("type");
		String code = request.getParameter("code");
		String name = request.getParameter("name");
		String value = request.getParameter("value");

		if(StringUtils.isNotBlank(name)){
			params.put("name", name);
		}

		if(StringUtils.isNotBlank(type)){
			params.put("type", type);
		}

		if(StringUtils.isNotBlank(code)){
			params.put("code", code);
		}

		if(StringUtils.isNotBlank(value)){
			params.put("value", value);
		}

		DataGrid dataGrid = new DataGrid();
		UserInfo u = ShiroUtils.getUserEntity();
		if(u.getType() == CommonConstants.UserInfoType.SYSTEM_USER){
			dataGrid = confInfoService.dataGrid(params, query.getPage(), query.getLimit());
		}

        return R.ok().put("page", dataGrid);
	}

	/**
	 * 列表
	 */
	@RequestMapping("/listAll")
	@RequiresPermissions("sys:confinfo:list")
	public List<ConfInfo> listAll(HttpServletRequest request){
		List<ConfInfo> confInfoList = new ArrayList<>();
		String type = request.getParameter("type");
		if(StringUtils.isNotBlank(type)){
			Map<String, Object> params = new HashMap<>();
			params.put("type", type);
			confInfoList = confInfoService.query(params);
		}
		return confInfoList;
	}



	/**
	 * 列表
	 */
	@RequestMapping("/selectAllTypes")
	@RequiresPermissions("sys:confinfo:list")
	public List<ConfInfo> selectAllTypes(){
		return confInfoService.selectAllTypes();
	}




	/**
	 * 根据ID获取数据
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("sys:confinfo:info")
	public R info(@PathVariable("id") Long id){
		ConfInfo confInfo = confInfoService.findById(id);
		return R.ok().put("confInfo", confInfo);
	}



	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("sys:confinfo:save")
	public R save(@RequestBody ConfInfo confInfo){
		confInfo.setCreateBy(ShiroUtils.getUserId());
		confInfo.setCreateTime(new Date());
		confInfoService.save(confInfo);

		return R.ok();
	}



	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("sys:confinfo:update")
	public R update(@RequestBody ConfInfo confInfo){
		ConfInfo c = confInfoService.findById(confInfo.getId());
		confInfo.setCreateTime(c.getCreateTime());
		confInfo.setCreateBy(c.getCreateBy());
		confInfo.setUpdateBy(ShiroUtils.getUserId());
		confInfo.setUpdateTime(new Date());
		confInfoService.update(confInfo);
		return R.ok();
	}



	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("sys:confinfo:delete")
	public R delete(@RequestBody Long[] ids){
		confInfoService.deleteBatch(ids);

		return R.ok();
	}
	
}
